<?php
// -----------------------------------------------------
// Created by : php-task
// -----------------------------------------------------
// User: tf <1096392101@qq.com>
// -----------------------------------------------------
declare (strict_types=1);

namespace Gao\Task\ipc;

//队列
trait Queue
{
    protected $queue = false;

    public function queue_init($queue_id)
    {

        if(!msg_queue_exists($queue_id)){ //检测队列id是否存在，即被使用
            $this->queue = msg_get_queue($queue_id,0666);
        }

        if ($this->queue === false) {
            die("create queue fail \n");
        }

        return $this->queue;
    }

    //设置消息对列消息
    public function queue_send($msg,$serialize = false,$blocking = true,$errCode = 0,$type = 1)
    {
        $ret = msg_send($this->queue,$type,$msg,$serialize,$blocking,$errCode);
    }

    //获取消息对列消息
    public function queue_receive($serialize = false,$flags = null,$errCode = 0,$msg_type = 1)
    {
        msg_receive($this->queue, 0, $msg_type, 1024, $message);
        return $message;
    }

    //销毁消息队列
    public function queue_remove()
    {
        msg_remove_queue($this->queue);
    }

}